// xc_lshop/club/clubshop/index.js
var actbill = require('../model/clubshop_pro')
var actclub_user = require('../model/club_user')
var actprobill = require('../../../xc/model/pro_contents')
var syscart = require('../../../xc/sys/cart')

const app = getApp()

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    navstatus:'0',
    CustomBar: app.globalData.CustomBar,
    totalnuminfo: {},
    xcpagesys: {},
    unit:"￥",
    yihuostatus:-1
  },
  loadcartinfo(clubid) {
    var that = this
    syscart.loadcartlocal(clubid).then(function name(resobj) {
      // console.log("resobj")
      // console.log(resobj)
      // for(var i in resobj.cart){
      //   console.log(resobj.cart[i])
      //   if(resobj.cart[i].protype==2 && clubid!=resobj.cart[i].clubid){
      //     resobj["totalnuminfo"] --;
      //     delete resobj[i]
      //   }
      // }
     
      that.setData({ cachecartinfo: resobj, totalnuminfo: resobj["totalnuminfo"] })
    })
  },
  loadpagesys() {
    var that = this;
    //xcpagesys
    setTimeout(() => {
      wx.createSelectorQuery().selectAll('#footer').boundingClientRect(function (rect) {
        var temupdate = {};
        temupdate["xcpagesys.foot"] = rect[0];
    
        that.setData(temupdate);
      }).exec()

    }, 500);
  },
  cartclosed() {
    var that = this
    that.loadcartinfo()
  },
  xcloaddata: function (options, config) {
    this.confgset(config)

    this.getclubinfo(options.clubid)
    this.loadpagesys();
  },
  confgset(config) {
    //主题设置
    var updateconfig = {};
    if (config.prothem && config.prothem.themcolor) {
      var themconfig = config.prothem.themcolor.config;
      updateconfig['themconfig'] = themconfig
    }
    
    if (config.prothem && config.prothem.detail) {
      var detailconfig = config.prothem.detail.proconfig;  
      var customStyle = config.prothem.detail.customStyle;
      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      updateconfig['detailconfig'] = detailconfig
      updateconfig['customStyle'] = customStyle
      updateconfig['popconfig'] = popconfig
      updateconfig['popCustomStyle'] = popCustomStyle
    }
    this.setData(updateconfig)
   
    if(config && config.yihuo && config.yihuo.status==1){
      if(config.payment && config.payment.balancepayunit){
        this.setData({
          unit:config.payment.balancepayunit,
          yihuostatus:config.yihuo.status
        })
      }
    }
   

      
  },
  buy(ev){
    if (!this.data.member.memberinfo) {
      this.login();
      return;
    }
    var that = this
    var dataset = ev.currentTarget.dataset
    var item = dataset.item
  
    var option = {protype:item.protype,clubid:item.shopid,clubmode:this.data.club.autarky==1?'4':'2'}; //团长产品
    console.log("option")
    console.log(option)
    console.log(item)
    // 1加购 2立即购买 3两种
    actprobill.showattrchose(that, item.protype==1?(item.platform_proid?item.platform_proid:item.id):item.id, null, 3,option)
  },
  cartsumbit(e){

    // 提交购买车订单
    var app =getApp();
    wx.navigateTo({
      url: '/' + app.globalData.xcsys.m + '/shop/paycore/payinfo?iscart=1&clubid='+ this.data.club.id +'&mode='+e.detail.clubmode+'&clubmust='+e.detail.ismust
    })
  },
  aftercart(e){
    this.loadcartinfo(this.data.club.id)

  },
  // 注册
  login() {
    var app = getApp()
    var that = this
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  afterlogin: function (options, config, userinfo) {
      this.setData({
        member: userinfo
      })
  },
  getclubinfo(id){
    var that = this
    actclub_user.getclubinfo(id).then(res => {
      if (res["status"] == 1) {
  
         var obj=res['obj'];
         that.setData({
           club:obj
         })
         that.loadcartinfo(id)

         if(that.data.navstatus==1 && obj.useplatformpro==1){
          that.getpropagelist(that, 1)
         }else{
          that.getpagelist(that, 1)
         }
      }else{
        that.error('团不存在',true)
      }
    })
  },
  sharegroup: function () {
    var widgetsshare = this.selectComponent("#widgetsshare");
    widgetsshare.showSharefunc()
  },
  onnavChange(e){
    var that=this;
    this.setData({
      navstatus:e.detail.name
    })
 
    if(that.data.navstatus==1 && that.data.club.useplatformpro==1){

      that.getpropagelist(that, 1)
     }else{
      that.getpagelist(that, 1)
     }
  },
  getcondition(){
    var condition={
      op: "offset",
      searchkey: "",
      status: 1
    }
    condition['type']=this.data.navstatus
    var club=this.data.club;
    condition['useplatformpro']=club.useplatformpro;
    condition['shopid']=club.id;

   
    return condition;
  },
  getpropagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }

      strwhere["status"] = 1;
      strwhere["bpid"] = 0;
      strwhere["spid"] = 0;
      strwhere["seachval"] = "";
      that.setData({ modellist: [] });
    }
    actprobill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

         for(var a in result["obj"]["modellist"] ){
           if(result["obj"]["modellist"][a]){
            result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
           }
         }     
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        }
        else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
     

    });
  },
  getpagelist(that, rest, condition) {
    if (rest == 1) {
      that.setData({ modellist: [] });
    }
    if(!condition){
      condition = this.getcondition();
    }
    actbill.offset(that, rest, condition).then(function (result) {
      var updatemodel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (result["obj"]["modellist"].length < 1 && that.data.modellist.length < 1) {

        } else if (rest == 1) {
          updatemodel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemodel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }

      wx.stopPullDownRefresh();
      that.setData(updatemodel);
    }, function () {
       
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    if(that.data.navstatus==1 && that.data.club.useplatformpro==1){
      that.getpropagelist(that, -1)
     }else{
      that.getpagelist(that, -1)
     }
  },
  onShareAppMessage: function () {

    var app = getApp()
    var sharetitle = this.data.club.title
    // var shareimg = this.data.shareimg
    var sharepath = app.xc_sys_config.getshareurl()
    console.log("onShareAppMessage")

    console.log(sharetitle)
    // console.log(shareimg)
    console.log(sharepath)

 
    return {
      title: sharetitle,
      path: sharepath,
      imageUrl: ""
    }
  },
  
})